package ge.telasi.tasks.ui.flowpermission;

import ge.telasi.tasks.model.FlowPermission;
import ge.telasi.tasks.ui.task.TaskDecontationUtils;
import java.awt.Component;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JList;

/**
 * @author dimitri
 */
public class FlowRoleCombo extends JComboBox {
    private static final long serialVersionUID = -1947454485422033318L;

    public FlowRoleCombo() {
        setRenderer(new MyRenderer());
    }

    public String getSelection() {
        Object selection = getSelectedItem();
        if (selection instanceof String) {
            return (String) selection;
        } else {
            return null;
        }
    }

    public void setOperation(int operation) {
        List<String> roles = FlowPermission.OPERATION_ROLES.get(operation);
        setModel(new DefaultComboBoxModel(roles.toArray()));
    }

    private class MyRenderer extends DefaultListCellRenderer {
        private static final long serialVersionUID = -3591135124262291257L;

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value instanceof String) {
                setText(TaskDecontationUtils.getRoleLocalizedName(value.toString()));
            }
            return this;
        }
        
    }

}
